#pragma once
#include <iostream>
#include <string>
#include <sys/types.h>
#include <sys/socket.h>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <cerrno>
#include <unistd.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include "protocal.hpp"
using namespace std;
class CalClient
{
public:
    CalClient(string Serverip, uint16_t Serverport) : _Serverip(Serverip), _Serverport(Serverport)
    {
    }
    ~CalClient()
    {
    }
    void Init()
    {
        // 创建socket
        _socketfd = socket(AF_INET, SOCK_STREAM, 0);
        if (_socketfd == -1)
        {
            cerr << "Client err" << errno << strerror(errno) << endl;
            exit(2);
        }
    }
    void run()
    {
        struct sockaddr_in peer;
        peer.sin_addr.s_addr = inet_addr(_Serverip.c_str());
        peer.sin_family = AF_INET;
        peer.sin_port = htons(_Serverport);
        socklen_t len = sizeof(peer);
        if (connect(_socketfd, (struct sockaddr *)&peer, len) != 0)
        {
            cout << "connnet error" << endl;
            exit(2);
        }
        string message;
        char buffer[1024];
        string inbuffer;
        while (1)
        {
            cout << "plase Enter# ";
            getline(cin, message);
            Request req = GetLine(message);
            string contest;
            req.serialization(&contest);
            string send_message = enlength(contest);
            send(_socketfd,send_message.c_str(),send_message.size(),0);            
            string package,text;
            if(!recvRequest(_socketfd,inbuffer,&package))
            continue;
            if(!delength(package,&text))
            continue;
            Resposne res;
            res.deserialization(text);
            cout<< "exitcode :"<<res._exitcode<<endl;
            cout<< "result :"<<res._result<<endl;
            
        }
    }
    Request GetLine(const string &text)
    {
        //status 0 符号前 1 符号中 2 符号后
        int status = 0;
        int count = text.size();
        string left,right;
        char op;
        int i = 0;
        while(i<count)
        {
            switch(status)
            {
                case 0:
                {
                    if(!isdigit(text[i]))
                    {
                        status = 1;
                        op = text[i];
                    }
                    else
                    {
                        left.push_back(text[i++]);
                    }
                    break;
                }
                case 1:
                {
                    i++;
                    status = 2;
                    break;
                }
                case 2:
                {
                    right += text[i++];
                    break; 
                }
            }
        }
        cout<<"left = "<<left<<endl;
        cout<<"right = "<<right<<endl;
        int x = stoi(left);
        int y = stoi(right);
        Request ret(x,y,op);
        return ret;
    }

private:
    int _socketfd;
    string _Serverip;
    uint16_t _Serverport;
};